package com.wzdigit.wms.wms.controller.TransferWareHouse;

import com.wzdigit.framework.exception.RestStatusException;
import com.wzdigit.framework.model.response.ErrorEntity;
import com.wzdigit.wms.common.utils.ValidatorUtil;
import com.wzdigit.wms.basic.client.Pager;
import com.wzdigit.wms.basic.client.PagerInfo;
import com.wzdigit.wms.basic.client.request.transfer.ConfirmInvZoneRequest;
import com.wzdigit.wms.basic.client.request.transfer.SearchIfTransferManageRequest;
import com.wzdigit.wms.basic.client.response.Result;
import com.wzdigit.wms.basic.client.response.transfer.AvailableInvZoneGroupWhsResponse;
import com.wzdigit.wms.basic.client.response.transfer.IfTransferManageDtlResponse;
import com.wzdigit.wms.basic.client.response.transfer.IfTransferManageResponse;
import com.wzdigit.wms.basic.client.response.transfer.TransferManageListResponse;
import com.wzdigit.wms.wms.constant.WmsWareConstant;
import com.wzdigit.wms.wms.service.transfer.IfTransferManageService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * OMS调拨任务拆分（调拨中间表管理）_PC 前端控制器
 * @author Administrator
 *
 */
@Slf4j
@RestController
@RequestMapping(value = WmsWareConstant.API_VERSION)
@Api(tags = "OMS调拨任务拆分（调拨中间表管理）_PC")
public class IfTransferManageController {

    public static final String REQUEST_MODEL = "/iftransfermanage";

    @Autowired
    private IfTransferManageService ifTransferManageService;

	/**
	 * 调拨中间表
	 * @param dto
	 * @param pager
	 * @param servletRequest
	 * @param servletResponse
	 * @return
	 */
	@ApiOperation(value = "调拨中间表",notes = "调拨中间表")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/searchIfTransferManage")
    public PagerInfo<List<IfTransferManageResponse>> searchIfTransferManage(
    		SearchIfTransferManageRequest dto, Pager<?> pager,
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
            return ifTransferManageService.searchIfTransferManage(dto, pager);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
	
	/**
	 * 调拨中间表明细
	 * @param ifTransferHeaderId
	 * @param servletRequest
	 * @param servletResponse
	 * @return
	 */
	@ApiOperation(value = "调拨中间表明细",notes = "调拨中间表明细")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/searchIfTransferManageDtl")
    public List<IfTransferManageDtlResponse> searchIfTransferManageDtl(
    		@ApiParam(value = "调拨中间表表头ID", required = true) 
    		@RequestParam(required = true) Integer ifTransferHeaderId, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
            return ifTransferManageService.searchIfTransferManageDtl(ifTransferHeaderId);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
	
	/**
	 * 根据OMS单据号获取调拨单列表
	 * @param sourceNo
	 * @param pager
	 * @param servletRequest
	 * @param servletResponse
	 * @return
	 */
	@ApiOperation(value = "根据OMS单据号获取调拨单列表",notes = "根据OMS单据号获取调拨单列表")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/searchTransferManage")
    public PagerInfo<List<TransferManageListResponse>> searchTransferManage(
    		@ApiParam(value = "OMS单据号", required = true) 
    		@RequestParam(required = true) String sourceNo, 
    		Pager<?> pager, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
            return ifTransferManageService.searchTransferManage(sourceNo, pager);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
	
	/**
	 * 获取可用库存
	 * @param ifTransferDtlId
	 * @param servletRequest
	 * @param servletResponse
	 * @return
	 */
	@ApiOperation(value = "获取可用库存",notes = "获取可用库存")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/getAvailableInvZone")
    public List<AvailableInvZoneGroupWhsResponse> getAvailableInvZone(
    		@ApiParam(value = "调拨中间表明细ID", required = true) 
    		@RequestParam(required = true) Integer ifTransferDtlId, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
            return ifTransferManageService.getAvailableInvZone(ifTransferDtlId, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
	
	/**
	 * 确认分配库存
	 * @param dto
	 * @param bindingResult
	 * @param servletRequest
	 * @param servletResponse
	 * @return
	 */
	@ApiOperation(value = "确认分配库存",notes = "确认分配库存")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PostMapping(value = REQUEST_MODEL + "/confirmInvZone")
    public Result<Integer> confirmInvZone(
    		@RequestBody @Valid ConfirmInvZoneRequest dto, 
    		BindingResult bindingResult, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
        	ValidatorUtil.validator(bindingResult);
            return ifTransferManageService.confirmInvZone(dto, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
	
	/**
	 * 取消分配库存
	 * @param gdnDtlId
	 * @param servletRequest
	 * @param servletResponse
	 * @return
	 */
	@ApiOperation(value = "取消分配库存",notes = "取消分配库存")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PutMapping(value = REQUEST_MODEL + "/cancelInvZone")
    public Result<?> cancelInvZone(
    		@ApiParam(value = "调拨中间表明细ID", required = true) 
    		@RequestParam(required = true) Integer ifTransferDtlId, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
            return ifTransferManageService.cancelInvZone(ifTransferDtlId, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
	
	/**
	 * 拆单
	 * @param ifTransferHeaderId
	 * @param servletRequest
	 * @param servletResponse
	 * @return
	 * @throws Exception 
	 */
	@ApiOperation(value = "拆单",notes = "拆单")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PutMapping(value = REQUEST_MODEL + "/split")
    public Result<String> split(
    		@ApiParam(value = "调拨中间表表头ID", required = true) 
    		@RequestParam(required = true) Integer ifTransferHeaderId, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) throws Exception {
        try {
            return ifTransferManageService.split(ifTransferHeaderId, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
}
